// pages/news/news.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number: 1,
  },
  coupon() {
    let name = getApp().globalData.usersname
    if (!name) {
      console.log('抱歉，您还未登录！')
      wx.showModal({
        title: '',
        content: '抱歉，您还未登录！',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            console.log('登录')
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else { //这里是点击了取消以后
            console.log('取消')
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/coupon/coupon',
      })
    }
  },
  goodsinfo(e) {
    var id = e.currentTarget.dataset.id
    // console.log(id)
    var popularity = e.currentTarget.dataset.popularity
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/shopping_popularity.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        id: id,
        popularity: popularity
      },
      success: function (res) {
        console.log(res.data)
        wx.navigateTo({
          url: '/pages/news-info/news-info?id=' + id + '',
        })
        that.setData({

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })

  },
  search: function (e) {
    wx.navigateTo({
      url: '/pages/news-search/news-search',
    })
  },
  addgoods(e) {
    var id = e.currentTarget.dataset.id
    var goodsimg = e.currentTarget.dataset.img
    var goodsname = e.currentTarget.dataset.name
    var goodsnews = e.currentTarget.dataset.news
    var goodsprice = e.currentTarget.dataset.price
    var shop = e.currentTarget.dataset.shop
    var type = e.currentTarget.dataset.type
    var name = getApp().globalData.usersname
    var num = e.currentTarget.dataset.number
    // console.log(this.data.hiddenName)
    // console.log(id, goodsimg, goodsname, goodsnews, goodsprice)
    if (!name) {
      console.log('抱歉，您还未登录！')
      wx.showModal({
        title: '提示',
        content: '是否进行用户登录',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            console.log('登录')
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else { //这里是点击了取消以后
            console.log('取消')
          }
        }
      })
    } else {
      console.log('检测到您已经登陆了！')
      var that = this;
      wx.request({
        url: 'http://localhost/shopping/shopping_if.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          username: name,
          goodsid: id,
        },
        success: function (res) {
          // console.log(res.data)

          if (res.data == '商品已存在') {
            console.log('商品已存在!!')
            wx.showModal({
              title: '',
              content: '商品已经加入购物车了!',
            })
          } else {
            console.log('商品不存在!!')
            if (num > 0) {
              wx.request({
                url: 'http://localhost/shopping/users_shopping_add.php',
                header: {
                  'Content-Type': 'application/json'
                },
                data: {
                  username: name,
                  id: id,
                  goodsimg: goodsimg,
                  goodsname: goodsname,
                  goodsnews: goodsnews,
                  goodsprice: goodsprice,
                  goodsshop: shop,
                  type: type,
                  number: that.data.number,
                  num: num,
                },
                success: function (res) {
                  // console.log(res.data)
                  console.log('加入购物车成功')
                  wx.showToast({
                    title: '成功加入购物车',
                    icon: 'success',
                    duration: 2000
                  })

                  that.setData({})
                },
                fail: function (res) {
                  console.log("fail");
                }
              })
            } else {
              console.log('商品库存不足!!')
              wx.showModal({
                title: '',
                content: '商品库存不足!',
              })
            }

          }
          that.setData({})
        },
        fail: function (res) {
          console.log("fail");
        }
      })
    }

  },


  navAllSwitch: function (e) {
    // console.log(e.currentTarget.dataset.index)
    let AllIndex = e.currentTarget.dataset.index;

    var that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_type_info.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: AllIndex
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrbb: res.data.info,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    that.setData({
      navAllState: AllIndex
    })

  },


  Goods: function () {
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_goods.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arr: res.data.goods,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/admin_type.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrxx: res.data.type,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/admin_type_info.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        name: that.data.navAllState
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrbb: res.data.info,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var typename = getApp().globalData.typename

    this.setData({
      navAllState: typename
    })
    this.Goods();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_goods.php',
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arr: res.data.goods,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/admin_type.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        that.setData({
          arrxx: res.data.type,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})